package com.adams.access.db.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

/**
 * 
 * 
 * File Name : DicTable.java
 * 
 * @Description : 表定义的基本信息 实体
 */
@Entity
@Table(name = "T_TABLE_INFO")
public class DicTable implements Serializable
{
	private static final long serialVersionUID = 1L;

	private String tablecode = "";// 表编码

	private String tableName;// 表名

	private String tabletype;// 表类型 0 数据库表，1 数据库视图 2虚拟表
	public static String TABLETYPE_DBT = "0";
	public static String TABLETYPE_DBV = "1";
	public static String TABLETYPE_DUMMY = "2";

	private String viewsql;// 数据库视图

	private String keycolumns;// 主键字段
	public static String COLUMNS_SEQ = ",";

	private String codecolumn;// 编码字段

	private String namecolumn;// 名称字段

	private String parentkeycolumn;// 上级关建字字段

	private String levelnocolumn;// 级次字段

	private String leafcolumn;// 是否末级字段

	private String ordercolumn;// 排序字段

	private String databasecode;// 数据库编码

	private boolean deletechilds;// 删除子级

	private String uniqueconstraints;// 唯一约束 字段与字段间以，号分隔 约束与约束之间以|分隔
	public static String CONSTRAINT_SEQ = "|";

	private String versioncolumn;// 版本控制字段

	@Id
	@GeneratedValue(generator = "assigned")
	@GenericGenerator(name = "assigned", strategy = "assigned")
	@Column(name = "TABLECODE", length = 50)
	public String getTablecode()
	{
		return tablecode;
	}

	public void setTablecode(String tablecode)
	{
		this.tablecode = tablecode;
	}

	@Column(name = "TABLENAME", length = 50)
	public String getTableName()
	{
		return tableName;
	}

	public void setTableName(String tableName)
	{
		this.tableName = tableName;
	}

	@Column(name = "TABLETYPE", length = 50)
	public String getTabletype()
	{
		return tabletype;
	}

	public void setTabletype(String tabletype)
	{
		this.tabletype = tabletype;
	}

	@Column(name = "VIEWSQL", length = 255)
	public String getViewsql()
	{
		return viewsql;
	}

	public void setViewsql(String viewsql)
	{
		this.viewsql = viewsql;
	}

	@Column(name = "KEYCOLUMNS", length = 255)
	public String getKeycolumns()
	{
		return keycolumns;
	}

	public void setKeycolumns(String keycolumns)
	{
		this.keycolumns = keycolumns;
	}

	@Column(name = "CODECOLUMN", length = 50)
	public String getCodecolumn()
	{
		return codecolumn;
	}

	public void setCodecolumn(String codecolumn)
	{
		this.codecolumn = codecolumn;
	}

	@Column(name = "NAMECOLUMN", length = 50)
	public String getNamecolumn()
	{
		return namecolumn;
	}

	public void setNamecolumn(String namecolumn)
	{
		this.namecolumn = namecolumn;
	}

	@Column(name = "PARENTKEYCOLUMN", length = 50)
	public String getParentkeycolumn()
	{
		return parentkeycolumn;
	}

	public void setParentkeycolumn(String parentkeycolumn)
	{
		this.parentkeycolumn = parentkeycolumn;
	}

	@Column(name = "LEVELNOCOLUMN", length = 50)
	public String getLevelnocolumn()
	{
		return levelnocolumn;
	}

	public void setLevelnocolumn(String levelnocolumn)
	{
		this.levelnocolumn = levelnocolumn;
	}

	@Column(name = "LEAFCOLUMN", length = 50)
	public String getLeafcolumn()
	{
		return leafcolumn;
	}

	public void setLeafcolumn(String leafcolumn)
	{
		this.leafcolumn = leafcolumn;
	}

	@Column(name = "ORDERCOLUMN", length = 50)
	public String getOrdercolumn()
	{
		return ordercolumn;
	}

	public void setOrdercolumn(String ordercolumn)
	{
		this.ordercolumn = ordercolumn;
	}

	@Column(name = "DATABASECODE", length = 50)
	public String getDatabasecode()
	{
		return databasecode;
	}

	public void setDatabasecode(String databasecode)
	{
		this.databasecode = databasecode;
	}

	@Column(name = "DELETECHILDS")
	public boolean getDeletechilds()
	{
		return deletechilds;
	}

	public void setDeletechilds(boolean deletechilds)
	{
		this.deletechilds = deletechilds;
	}

	@Column(name = "UNIQUECONSTRAINTS")
	public String getUniqueconstraints()
	{
		return uniqueconstraints;
	}

	public void setUniqueconstraints(String uniqueconstraints)
	{
		this.uniqueconstraints = uniqueconstraints;
	}

	@Column(name = "VERSIONCOLUMN")
	public String getVersioncolumn()
	{
		return versioncolumn;
	}

	public void setVersioncolumn(String versioncolumn)
	{
		this.versioncolumn = versioncolumn;
	}

	/**
	 * Description : 表是否使用复合主键
	 * 
	 * @return
	 * 
	 */
	@Transient
	public boolean isCompositeKey()
	{
		boolean result = false;
		if (keycolumns.indexOf(COLUMNS_SEQ) > -1)
		{
			result = true;
		}
		return result;
	}

	/**
	 * Description : 得到主键字段列表
	 * 
	 * @return
	 * 
	 */
	@Transient
	public List<String> getKeycolumnlist()
	{
		List<String> result = new ArrayList<String>();
		if (null != keycolumns && !"".equals(keycolumns.trim()))
		{
			StringTokenizer st = new StringTokenizer(keycolumns, COLUMNS_SEQ);
			while (st.hasMoreTokens())
			{
				String column = st.nextToken();
				if (null != column && column.trim().length() > 0)
				{
					result.add(column);
				}
			}
		}
		return result;
	}

	/**
	 * Description : 删除关键字段
	 * 
	 * @param columncode
	 * 
	 */
	@Transient
	public void removeKeycolumn(String columncode)
	{
		List<String> keycolumns = getKeycolumnlist();
		int len = COLUMNS_SEQ.length();
		StringBuilder sb = new StringBuilder();
		for (String keycolumn : keycolumns)
		{
			if (null != columncode && !columncode.equals(keycolumn))
			{
				sb.append(keycolumn);
				sb.append(COLUMNS_SEQ);
			}
			sb.delete(0, sb.length() - len - 1);
		}
		this.setKeycolumns(sb.toString());
	}

	/**
	 * Description : 得到唯一约束列表 列表主键字段列表
	 * 
	 * @return
	 * 
	 */
	@Transient
	public List<Set<String>> getUniqueconstraint()
	{
		List<Set<String>> result = new ArrayList<Set<String>>();
		if (null != uniqueconstraints && uniqueconstraints.trim().length() > 0)
		{
			StringTokenizer constraintSt = new StringTokenizer(
					uniqueconstraints, CONSTRAINT_SEQ);
			while (constraintSt.hasMoreTokens())
			{
				String columns = constraintSt.nextToken();
				if (null != columns && columns.trim().length() > 0)
				{
					StringTokenizer columnSt = new StringTokenizer(columns,
							COLUMNS_SEQ);
					Set<String> columnSet = new HashSet<String>();
					while (columnSt.hasMoreTokens())
					{
						String column = columnSt.nextToken();
						columnSet.add(column);
					}
					result.add(columnSet);
				}
			}
		}
		return result;
	}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + tablecode.hashCode();
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final DicTable other = (DicTable) obj;
		if (tablecode.equals(other.tablecode))
			return false;
		return true;
	}

}
